package com.goldenautumn.prefabricate.activity.mine

import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LanguageUtils
import com.blankj.utilcode.util.LogUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.mine.adapter.AddressAdapter
import com.goldenautumn.prefabricate.activity.mine.adapter.LanguageAdapter
import com.goldenautumn.prefabricate.business.entity.LanguageInfoVo
import com.goldenautumn.prefabricate.business.entity.OrderMenuInfoVo
import com.goldenautumn.prefabricate.business.user.UserInfoUiState
import com.goldenautumn.prefabricate.business.user.UserViewModel
import com.goldenautumn.prefabricate.databinding.ActivityLanguageBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.goldenautumn.prefabricate.utils.loadImgUrl
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseActivity
import qncx.external.common.compat.visible
import java.util.Locale

class LanguageActivity : BaseActivity<ActivityLanguageBinding>() {

    private val mUserModel by viewModels<UserViewModel>()

    private val mAdapter by lazy {
        LanguageAdapter()
    }

    override fun getLayoutId(): Int  = R.layout.activity_language

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        mBinding.toolbar.setNavigationOnClickListener { onBackPressedDispatcher.onBackPressed() }

        mBinding.languageRv.layoutManager = LinearLayoutManager(this)
        mBinding.languageRv.adapter = mAdapter

        mAdapter.setOnItemClickListener{adapter,_,position ->
            var item = adapter.getItem(position)
            mAdapter.selectPosition = position
            mAdapter.notifyDataSetChanged()
            item?.locale?.let { LanguageUtils.applyLanguage(it,true) }
        }

        mUserModel.getLanguage()

        while (isActive) {
            select {
                mUserModel.user.onReceive {
                    when(it) {
                        is UserInfoUiState.LanguageList -> {
                            mAdapter.submitList(it.language)
                            var language = if(LanguageUtils.isAppliedLanguage()) LanguageUtils.getAppliedLanguage() else LanguageUtils.getSystemLanguage()
                            it.language.forEachIndexed { index, languageInfoVo ->
                                if(languageInfoVo.type == language.language.lowercase()){
                                    mAdapter.selectPosition = index
                                } else if(languageInfoVo.type.contains(language.country.lowercase()) && language.language.lowercase() == "zh"){
                                    mAdapter.selectPosition = index
                                }
                            }
                            mAdapter.notifyDataSetChanged()
                        }
                        else -> {}
                    }
                }
            }
        }
    }


}